package com.assumejson.transaction.cases;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.logging.Logger;

/**
 * 事务隔离级别案例分析
 *
 * @author gang.Li
 * @version 1.0
 * @since 1.0 2021/12/16 11:40
 */
@Component
public class CaseStudy {

	private final static Logger log = Logger.getLogger(CaseStudy.class.getName());

	@Autowired
	private CaseStudy caseStudy;

	@Transactional
	public void test() {
		log.info("test sql is running....");
		caseStudy.a();
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void a() {
		log.info("a sql is running....");
		int i = 100 / 0;
	}

	@Transactional
	public void test1() {
		try {
			b();
		} catch (Exception e) {
			log.info("sql is running failed.Roll back immediately");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
	}

	public void b() {
		throw new RuntimeException("sql is failed");
	}
}
